PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

Checking the Classes of Subroutine Parameters

You cannot specify the class of a parameter in a subroutine definition. You can, however, get the value of the Class property of a parameter and check it to see if the parameter belongs to the correct class. If it doesn't, you may be able to coerce it with the As operator, or failing that, you can return an error. For information about coercing values, see Expressions. For information about returning errors, see Try Statements.

Here's an example of a subroutine that checks to see if its parameter is a real number or an integer. If not, the routine uses the Error command to signal an error.

on areaOfCircle from radius
    -- Make sure the parameter is a real number or an integer.
    if class of radius is contained by {integer, real}
        return radius * pi  -- pi is predefined by AppleScript.
    else
        error "The parameter must be a real number or an integer"
    end if
end areaOfCircle

-- To call areaOfCircle:
areaOfCircle from 7 --result: 21.991148575129

© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)